home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xfcn / spttool.cpt / Support Tools eXternals 1.2.5 / card_38961.txt < prev    next >
Text File  |  1990-11-13  |  12KB  |  360 lines

  1. -- card: 38961 from stack: in.5
  2. -- bmap block id: 44053
  3. -- flags: 0000
  4. -- background id: 3858
  5. -- name: OpenFiles
  6. ----- HyperTalk script -----
  7. on CloseCard
  8.   put empty into cd fld "file list"
  9.   set the scroll of cd fld "file list" to 0
  10.   pass CloseCard
  11. end CloseCard
  12.  
  13. on HideObjects
  14.   hide cd fld "file list"
  15.   hide cd btn "try it!"
  16. end HideObjects
  17.  
  18. on ShowObjects
  19.   show cd fld "file list"
  20.   show cd btn "try it!"
  21. end ShowObjects
  22.  
  23.  
  24. -- part 1 (button)
  25. -- low flags: 00
  26. -- high flags: A002
  27. -- rect: left=82 top=292 right=326 bottom=175
  28. -- title width / last selected line: 0
  29. -- icon id / first selected line: 0 / 0
  30. -- text alignment: 1
  31. -- font id: 0
  32. -- text size: 12
  33. -- style flags: 8192
  34. -- line height: 16
  35. -- part name: Try It!
  36. ----- HyperTalk script -----
  37. on mouseUp
  38.   global errGlobal
  39.   put volumePath() into newVolume
  40.   if newVolume = empty then exit mouseUp
  41.   put OpenFiles(newVolume, "noDialog:errGlobal") into fileList
  42.   if errGlobal Γëá empty then
  43.     answer "Error:" && errGlobal
  44.     put empty into errGlobal
  45.   else
  46.     put fileList into cd fld "file list"
  47.   end if
  48. end mouseUp
  49.  
  50.  
  51.  
  52.  
  53. -- part 2 (field)
  54. -- low flags: 00
  55. -- high flags: 0007
  56. -- rect: left=3 top=117 right=288 bottom=256
  57. -- title width / last selected line: 0
  58. -- icon id / first selected line: 0 / 0
  59. -- text alignment: 0
  60. -- font id: 4
  61. -- text size: 9
  62. -- style flags: 0
  63. -- line height: 12
  64. -- part name: file list
  65.  
  66.  
  67. -- part contents for background part 38
  68. ----- text -----
  69. 34/50
  70.  
  71. -- part contents for background part 20
  72. ----- text -----
  73.      An XFCN which returns a list of  all open files on a specified volume.  Files are listed as full-path-names.
  74.  
  75.      Calling syntax : OpenFiles(volName, <ΓÇ£noDialogΓÇ¥:errorGlobal>)
  76.   VOLNAME: the volume to check.  
  77.  
  78. NOTE:  Sometimes you will see the same file listed twice.  This is usually caused by the data and resource forks being open simultaneously.
  79.  
  80. -- part contents for background part 42
  81. ----- text -----
  82. { OpenFiles(volumeName)                    }
  83. { XFCN returns a list of the open files on the specified     }
  84. {volume.  Get the list by stepping through the FCBs.    }
  85. {}
  86. {  brought to you by:  Anup Murarka      Eric Carlson    }
  87. {            ALINK:  SKEPTIC      ALINK:  cyNic  }
  88. {                  CIS:  76004,3356    }
  89. {}
  90. {        We are part of the Support Tools Development Group,  }
  91. {        Apple Computer, Inc.   }
  92. {}
  93. {        please DO NOT contack Mac DTS for support of this code!  }
  94. {}
  95. {        please DO contact the authors for support of this code!  }
  96. {}
  97. {        Send comments, bug reports, requests to any of the above  }
  98. {        E-mail addresses or to:}
  99. {}
  100. {              (one of us)          }
  101. {              Apple Computer, Inc.     }
  102. {              900 E. Hamilton, Ave.    }
  103. {              Campbell, CA   95008    }
  104. {              M/S 72-L          }
  105. {}
  106. {  Copyright:  ┬⌐ 1989, 1990 by Apple Computer, Inc., all rights reserved.  }
  107. {}
  108. { written by Eric Carlson                    }
  109. { AppleLink:  cyNic                        }
  110. { modification history                      }
  111. {       Date        Initials                  Comments              }
  112. {       ----        ------  ------------------------------------------------------}
  113. {    1/7/90      ec       first written                            }
  114. {    8/28/90      ec      added additional error checking when extending handle, changed}
  115. {                    structure of loop.  changed version to 1.1  }
  116. {}
  117.  
  118. unit OpenFiles;
  119.  
  120. interface
  121.  
  122.   uses
  123.     HyperXCMD;
  124.  
  125.   procedure MAIN (paramPtr: XCmdPtr);
  126.  
  127. implementation
  128.  
  129.   function AppendString (h: Handle;
  130.                   newStr: Str255): OSErr;
  131.   { stick the string onto the back of the handle }
  132.   begin
  133.     AppendString := PtrAndHand(Ptr(ORD4(@newStr) + 1), h, LENGTH(newStr));
  134.   end;
  135.  
  136.   procedure reportToUser (paramPtr: XCmdPtr;
  137.                   msgStr: str255);
  138. {}
  139. { report something back to the user.  }
  140. { the last parameter (optional) to an external may contain }
  141.  { "noDialog" or "noDialog:GlobalName".  GlobalName is the name }
  142.  { of a HyperTalk global variable into which error messages will be }
  143.  { placed.  we've decided to use this approach to avoid confusing }
  144. { an error message with a valid result being returned from an XFCN. }
  145. {}
  146.     var
  147.       tempStr: str255;
  148.   begin
  149. {check the last param to see if the user requested that}
  150. { we suppress the error dialog }
  151.     ZeroToPas(paramPtr, paramPtr^.params[paramPtr^.paramCount]^, tempStr);
  152.     UprString(tempStr, true);
  153.     if pos('NODIALOG', tempStr) = 0 then
  154.   { no special error handling specified, throw up a dialog and return the error message }
  155.       begin
  156.         SendCardMessage(paramPtr, concat('answer "', msgStr, '"'));
  157.         paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  158.       end
  159.     else if (pos(':', tempStr) > 0) then
  160.   { requested global AND noDialog so we fill in the global and return empty }
  161.       begin
  162.         tempStr := copy(tempStr, pos(':', tempStr) + 1, length(tempStr));
  163.                             { get the name of the HC global  to fill }
  164.         SetGlobal(paramPtr, tempStr, PasToZero(paramPtr, msgStr));
  165.                             { and fill it }
  166.         paramPtr^.returnValue := PasToZero(paramPtr, '');  { return empty }
  167.       end
  168.     else
  169.   { requested noDialog only so we return the error condition as the result }
  170.       paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  171.   end;  { procedure }
  172.  
  173.   function askedForHelp (paramPtr: XCmdPtr;
  174.                   syntaxMsg: Str255;
  175.                   copyRightMsg: Str255): boolean;
  176. {  check to see if the user sent a '?' or a '!' as }
  177. { the only parameter. if so we will respond with }
  178. { the calling syntax or the copyright/version info }
  179. { for this external }
  180. {}
  181.     var
  182.       firstStr: str255;
  183.   begin
  184.     askedForHelp := false;
  185.     if paramPtr^.paramCount = 1 then
  186.       begin
  187.         ZeroToPas(paramPtr, paramPtr^.params[1]^, firstStr);
  188.           { what is the first param? }
  189.         if firstStr = '?' then
  190.           begin
  191.             reportToUser(paramPtr, syntaxMsg);
  192.             askedForHelp := true
  193.           end  { asked for help }
  194.         else if firstStr = '!' then
  195.           begin
  196.             reportToUser(paramPtr, copyRightMsg);
  197.             askedForHelp := true
  198.           end;  { asked for copyright info }
  199.       end;  { one parameter passed }
  200.   end;    { function }
  201.  
  202.   function getVolRefNum (pathName: str255): integer;
  203.   { function to return the volume reference number of the volume specified in the pathName}
  204.   { parameter.  Will automatically strip any trailing directory/file names, or add a colon to }
  205.   { the volume name.  MaxInt is returned if an error is encountered. }
  206.     var
  207.       paramBlock: HParamBlockRec;
  208.       errorCode: OSerr;
  209.   begin
  210. { a path name must have a colon on the end }
  211.     pathName := concat(pathName, ':');
  212.     pathName := copy(pathName, 1, pos(':', pathName));
  213.     with paramBlock do
  214.       begin
  215.         ioCompletion := nil;
  216.         ioNamePtr := @pathName;
  217.         ioVRefNum := 0;
  218.         ioVolIndex := -1;
  219.     { if volindex is zero the file manager will try to get to the volume}
  220.     { through the ioVRefNum ΓÇö not a good thing here as that is what we don't know! }
  221.       end;
  222.     errorCode := PBHGetVInfo(@paramBlock, FALSE);
  223.     if errorCode <> noErr then
  224.       getVolRefNum := maxInt
  225.     else
  226.       getVolRefNum := paramBlock.ioVRefNum;
  227.   end;
  228.  
  229.   function BitTest (AddressToCheck: ptr;
  230.                   TotalBits: integer;
  231.                   BitToTest: longint): boolean;
  232.   { function that allows caller to use std. 68000 bit notation instead of the Toolbox's reversed notation}
  233.   { example:  bit 0 (the least significant bit) in a byte is bit 7 in the Toolbox's notation}
  234.   begin
  235.     BitTest := BitTst(AddressToCheck, TotalBits - 1 - BitToTest);
  236.   end;
  237.  
  238.   function AUXisRunning: boolean;
  239.     const
  240.       HWCfgFlag = $0B22;
  241.   begin
  242.     AUXisRunning := BitTest(pointer(HWCfgFlag), 16, 9);
  243.   end;
  244.  
  245.   function PathNameFromDirID (dirID: longint;
  246.                   vRefnum: integer;
  247.                   var fullPathName: str255): OSErr;
  248. { build up a full path name given a directory id and an vol ref num.  this method isn't reccomended in general (see the }
  249. {  various tech notes, but we use it in HC externals as HC uses exclusively full path names }
  250.     var
  251.       myCPB: CInfoPBRec;
  252.       directoryName: str255;
  253.       err: OSErr;
  254.   begin
  255.     fullPathName := '';
  256.     with myCPB do
  257.       begin
  258.         ioNamePtr := @directoryName;
  259.         ioDrParID := DirId;
  260.       end;
  261.  
  262.     repeat
  263.       with myCPB do
  264.         begin
  265.           ioVRefNum := vRefNum;
  266.           ioFDirIndex := -1;
  267.           ioDrDirID := myCPB.ioDrParID;
  268.         end;
  269.       err := PBGetCatInfo(@myCPB, FALSE);
  270.  
  271.       directoryName := concat(directoryName, ':');
  272.  
  273. { pascal strings mustn't be longer than 255 chars, though a path name may, so check }
  274.       if length(directoryName) + length(fullPathName) <= 255 then
  275.         fullPathName := concat(directoryName, fullPathName)
  276.       else
  277.         myCPB.ioDrDirID := fsRtDirID;    { lazy persons way to jump out }
  278.  
  279.     until (myCPB.ioDrDirID = 2);
  280.     PathNameFromDirID := err;
  281.   end;
  282.  
  283.   procedure OpenFiles (paramPtr: XCmdPtr);
  284.     var
  285.       FP: FCBPBRec;
  286.       volRefNum, fileNdx: integer;
  287.       requestedVolName, fileName, filePath: str255;
  288.       fileErr, err: OSErr;
  289.       fileList: handle;
  290.   begin
  291.     if askedForHelp(paramPtr, 'OpenFiles(volumeName,<ΓÇ£noDialogΓÇ¥:errorGlobal>)', 'v1.0, ┬⌐ 1990  Apple Computer, Inc., Eric Carlson') then
  292.       exit(OpenFiles);
  293.  
  294.     if paramPtr^.paramCount < 1 then          { we need the disk name to search for }
  295.       begin
  296.         reportToUser(paramPtr, 'Disk name expected');
  297.         exit(OpenFiles)
  298.       end;
  299.     ZeroToPas(paramPtr, paramPtr^.params[1]^, requestedVolName);
  300.  
  301.     volRefNum := GetVolRefNum(requestedVolName);    { get the vol ref num }
  302.     if volRefNum = maxInt then
  303.       begin
  304.         ReportToUser(paramPtr, 'Volume not found.');
  305.         exit(OpenFiles);
  306.       end;
  307.  
  308.     fileList := NewHandle(0);          { allocate a handle for the file list }
  309.     err := MemError;
  310.     if err <> noErr then
  311.       begin
  312.         reportToUser(paramPtr, 'Out of memory.');
  313.         if fileList <> nil then
  314.           DisposHandle(fileList);
  315.         exit(OpenFiles);
  316.       end;
  317.  
  318.     zeroBytes(paramPtr, @FP, sizeOf(FP));
  319.     FP.ioCompletion := nil;              { don't want async }
  320.     FP.ioVRefNum := volRefNum;          { only files on the specified volume }
  321.     FP.ioNamePtr := @fileName;          { the file name }
  322.     FP.ioFCBIndx := 0;                { start with the first file }
  323.     repeat                      { loop through all files}
  324.       FP.ioFCBIndx := FP.ioFCBIndx + 1;      { go to the next file }
  325.       fileErr := PBGetFCBInfo(@FP, false);    { check the next file }
  326.       if fileErr = noErr then
  327.         fileErr := PathNameFromDirID(FP.ioFCBParID, FP.ioVRefNum, filePath);  { build the path }
  328.       if fileErr = noErr then
  329.         begin
  330.           filePath := concat(filePath, fileName, chr(13));    { add the file name, CR }
  331.           err := AppendString(fileList, filePath);          { remember the file name }
  332.           if err <> noErr then
  333.             begin
  334.               reportToUser(paramPtr, 'Out of memory.');
  335.               if fileList <> nil then
  336.                 DisposHandle(fileList);
  337.               exit(OpenFiles);
  338.             end;
  339.         end;
  340.     until (fileErr <> noErr);
  341.     if fileList <> nil then
  342.       begin
  343.         SetHandleSize(fileList, GetHandleSize(fileList) - 1);  { drop the trailing CR }
  344.         fileErr := AppendString(fileList, chr(0));        { Terminate with 0 byte  }
  345.         if fileErr <> noErr then
  346.           begin
  347.             reportToUser(paramPtr, 'Out of memory.');
  348.             if fileList <> nil then
  349.               DisposHandle(fileList);
  350.           end;
  351.         paramPtr^.returnValue := fileList;
  352.       end;
  353.   end;
  354.  
  355.   procedure MAIN (paramPtr: XCmdPtr);
  356.   begin
  357.     OpenFiles(paramPtr);
  358.   end;
  359.  
  360. end.  { unit OpenFiles}